package com.gss.autotools.util;

import com.gss.autotools.entity.WindowPointInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author: DYang
 * @since: 2023/5/26
 * @description:
 */
public class ClickUtils {

    public static WindowPointInfo getWindowPoint(){
        WindowPointInfo info = new WindowPointInfo();
        Point point = MouseInfo.getPointerInfo().getLocation();
        double x = point.getX();
        double y = point.getY();
        info.setPointX(x);
        info.setPointY(y);
        return info;
    }

    private static int number = 0;

    public static void main(String[] args) {
        // 创建一个MouseListener对象
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // 鼠标按下时将数字加1
                number++;
                WindowPointInfo windowPoint = getWindowPoint();
                System.out.println(windowPoint.getPointX() + " , " + windowPoint.getPointY());
                System.out.println("Number: " + number);
            }
        };

        // 在需要监听鼠标点击的组件上添加MouseListener
        // 这里以一个JFrame为例
        javax.swing.JFrame frame = new javax.swing.JFrame();
        frame.addMouseListener(mouseAdapter);

        // 其他初始化和显示窗口的代码
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

}
